******************************************************
* Compare Liquidity measures and estimate elasticity *
******************************************************
 
use "$Replicationdirectory/_Work/TEMP/PANEL_incl_liquidity_ytm", clear


* create output directory


		capture mkdir "$Replicationdirectory/_Work/Output/TABS/TAB_3"



* prepare data / summary statistics 

		gen spread_liquidity_overconsol=ROLL-ROLL_consol_london
		
		gen spread_DAWES_CONSOL=ytm-y_consol_London
		
		
		sum ROLL_consol_london if default==1
		sum ROLL		 if default==1 & market=="london"
		sum ROLL		 if default==1 & market=="paris"
		sum spread_liquidity  if default==1 & market=="paris"
		sum ROLL		 if default==1 & market=="amsterdam"		
		sum spread_liquidity  if default==1 & market=="amsterdam"
		sum ROLL		 if default==1 & market=="zurich"
		sum spread_liquidity  if default==1 & market=="zurich"
		
		sum ROLL_consol_london if default==1
		
		* gen fixed effects	
		egen year_market=group(year market_enc)
		egen month_market=group(myear market_enc)
		egen week_market=group(wyear market_enc)
   
		
		
		
		sum spread_ytm if default==1 & market=="paris"
		sum spread_ytm if default==1 & market=="amsterdam"
		sum spread_ytm if default==1 & market=="zurich"
 
  

  
 * Regression  
  
		* Column (1): No extrapolated data 
 		 
		eststo r1: reghdfe spread_ytm 	spread_liquidity  if    default==1 & market!="london"  & day_in_sample<1930   ,absorb(month_market date ) cluster(month_market date) level(90)
 		
		
    
   
					* use elasticity to calculate liquidity premium over London Dawes (Table 2)
				
					 
					
					gen 	lq_premium_point=_b[spread_liquidity]*spread_liquidity 	if    			default==1 & market!="london"
					gen 	lq_premium_upper=(_b[spread_liquidity] +invttail(e(df_r),0.05)*_se[spread_liquidity]) *spread_liquidity	if    			default==1 & market!="london"
					gen 	lq_premium_lower=(_b[spread_liquidity] - invttail(e(df_r),0.05)*_se[spread_liquidity] )*spread_liquidity	if    			default==1 & market!="london"
					
					
					foreach x in point upper lower {
						replace lq_premium_`x'=.										if    			default==1 & market=="london" 
					}
					
					
					 
					 sum 	lq_premium_point 									 		if    			default==1 & market=="paris",
					 sum 	lq_premium_lower 									 		if    			default==1 & market=="paris",
					 sum 	lq_premium_upper 									 		if    			default==1 & market=="paris",
					 
					 
					 sum 	lq_premium_point 											if    			default==1 & market=="amsterdam",  
					 sum 	lq_premium_lower 											if    			default==1 & market=="amsterdam",  
					 sum 	lq_premium_upper 											if    			default==1 & market=="amsterdam",  
				 
					 
					 
					 sum 	lq_premium_point											if    			default==1 & market=="zurich",  
					 sum 	lq_premium_lower											if    			default==1 & market=="zurich",  
					 sum 	lq_premium_upper											if    			default==1 & market=="zurich",  
		
				  
				  
					* export and drop (as most conservative estimate will be used for later calcuations)
				  
 					
					foreach x in  paris amsterdam zurich{
						eststo clear 
						eststo: quietly estpost summarize   lq_premium_point lq_premium_lower lq_premium_upper if default==1 & market=="`x'"
						esttab using "$Replicationdirectory/_Work/Output/TABS/TAB_3/Col1_Bounds_`x'.rtf", cells("mean(fmt(%5.3f))") label nodepvar  noobs replace  
					}
				
				  
					 drop lq_premium_point lq_premium_upper lq_premium_lower
		
		
		
		
							eststo clear 

		
		* Column (2):  Last available spread
		eststo r2: reghdfe spread_ytm_last 	spread_liquidity  	if 	default==1 & market!="london"  & day_in_sample<1930  ,absorb(month_market date ) cluster(month_market date)	level(90)

 
		
		
		 
 		
					* 	* use elasticity to calculate liquidity premium over London Dawes (Table 2 - lower part)
		
					
					gen 	lq_premium_point=_b[spread_liquidity]*spread_liquidity 	if    			default==1 & market!="london"
					gen 	lq_premium_upper=(_b[spread_liquidity] +invttail(e(df_r),0.05)*_se[spread_liquidity]) *spread_liquidity	if    			default==1 & market!="london"
					gen 	lq_premium_lower=(_b[spread_liquidity] - invttail(e(df_r),0.05)*_se[spread_liquidity] )*spread_liquidity	if    			default==1 & market!="london"
					
					
					foreach x in point upper lower {
						replace lq_premium_`x'=0											if    			default==1 & market=="london" 
					}
					
					
					 
					 sum 	lq_premium_point 									 		if    			default==1 & market=="paris",
					 sum 	lq_premium_lower 									 		if    			default==1 & market=="paris",
					 sum 	lq_premium_upper 									 		if    			default==1 & market=="paris",
					  
					 
					 sum 	lq_premium_point 											if    			default==1 & market=="amsterdam",  
					 sum 	lq_premium_lower 											if    			default==1 & market=="amsterdam",  
					 sum 	lq_premium_upper 											if    			default==1 & market=="amsterdam",  
				 
					   
					 
					 sum 	lq_premium_point											if    			default==1 & market=="zurich",  
					 sum 	lq_premium_lower											if    			default==1 & market=="zurich",  
					 sum 	lq_premium_upper											if    			default==1 & market=="zurich",  

					   
					
					* export for Table 
				  
					
					foreach x in  paris amsterdam zurich{
						eststo clear 
						eststo: quietly estpost summarize   lq_premium_point lq_premium_lower lq_premium_upper if default==1 & market=="`x'"
						esttab using "$Replicationdirectory/_Work/Output/TABS/TAB_3/Col2_Bounds_`x'.rtf", cells("mean(fmt(%5.3f))") label nodepvar  noobs replace  
					}
				
				  
 		
		
		* export upper part of the table (rerun regressions as eststo clear was used )
		eststo clear 
		eststo r1: reghdfe spread_ytm 	spread_liquidity  		if  default==1 & market!="london"  & day_in_sample<1930  ,absorb(month_market date ) cluster(month_market date) level(90)		
		eststo r2: reghdfe spread_ytm_last 	spread_liquidity if default==1 & market!="london"  & day_in_sample<1930  ,absorb(month_market date ) cluster(month_market date)	 level(90)

							esttab using "$Replicationdirectory/_Work/Output/TABS/TAB_3/Regressioncoefficients.rtf", replace  nogap star( * 0.1 ** 0.05 *** 0.01) se ar2

* Export for decomposition 
		
					
					* LQP of senior bond over risk-free rate 
					* use elasticity to calculate liquidity premium over London Consol assuming upper bound of most conservative specification (add +invttail(e(df_r),0.05))

						* rerun regression because eststo clear was used in between. 	
						eststo clear 
						eststo r2: reghdfe spread_ytm_last 	spread_liquidity if default==1 & market!="london"  & day_in_sample<1930  ,absorb(month_market date ) cluster(month_market date)	 level(90)

										preserve 
										
										keep if market=="london"
										
										gen lq_premium_consol=(_b[spread_liquidity]+invttail(e(df_r),0.05) *_se[spread_liquidity])*spread_liquidity_overconsol 	
										
 										
										  
										
										keep market_enc day_in_sample lq_premium_consol
										
										save "$Replicationdirectory/_Work/TEMP/liquidity_over_consol", replace 
										restore 

										
	
		
		
     
					*	Smooth these liquidity premia for graph 
								merge m:1 day_in_sample using "$Replicationdirectory/_Work/TEMP/liquidity_over_consol" , nogen 

								 preserve 
								
										
										tssmooth ma LP_OVER_Cons_ = lq_premium_consol,  	window(15 1 15)
										
										tssmooth ma LP_OVER_SeniorDawes_ = lq_premium_upper,  		window(15 1 15)
										
										replace LP_OVER_SeniorDawes_=0 if default==0
											
										keep market date LP_OVER_Cons_  LP_OVER_SeniorDawes_
										reshape wide    LP_OVER_Cons_   LP_OVER_SeniorDawes_, i(date) j(market)  string
										
										
										
										save "$Replicationdirectory/_Work/TEMP/LQP", replace 
										   
							 	restore 		
								
								 
								 
		
